﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace FileDuplicateUtility
{
    public class File
    {
        private static readonly string[] SizeSuffixes = new[] { "B", "KB", "MB", "GB", "TB" };

        public File(Folder parent, string name, long size)
        {
            Parent = parent;
            Name = name;
            Size = size;
        }

        public Folder Parent { get; private set; }
        public string Name { get; private set; }
        public long Size { get; private set; }

        public string FullPath { get { return Parent == null ? Name : System.IO.Path.Combine(Parent.FullPath, Name); } }
        public System.IO.FileInfo FileInfo { get { return new System.IO.FileInfo(FullPath); } }

        public string FileSize { get { return SizeString(Size); } }

        internal static string SizeString(long size)
        {
            var i = 0;
            double dsize = size;
            while (i < SizeSuffixes.Length && dsize > 1024)
            {
                dsize /= 1024;
                ++i;
            }

            return string.Format("{0:0.00} {1}", dsize, SizeSuffixes[i]);
        }

        public override string ToString()
        {
            return string.Format("{0} ({1})", FullPath, FileSize);
        }
    }

    public class SizeConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string converted = null;

            if (value is long)
            {
                converted = File.SizeString((long)value);
            }
            else
            {
                converted = value.ToString();
            }

            return converted;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

}
